package com.dbq.fast.business.order.apdapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.order.bean.OrderInfo;

import java.util.List;


public class OrderListAdapter extends RecyclerView.Adapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    private List<OrderInfo.OrderListBean> orderInfos;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_ITEM){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order, parent,false);
            return new ItemViewHolder(view);
        }else {
            FooterViewHolder footerVH = new FooterViewHolder(LayoutInflater.from(App.getContext()).inflate(R.layout.item_list_footer, parent, false));
            return footerVH;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
            ItemViewHolder itemVH = (ItemViewHolder) holder;
            OrderInfo.OrderListBean orderListBean = orderInfos.get(position);
            if(orderListBean != null){
                itemVH.orderPkg.setText(orderListBean.getServeName());
                String statusStr = "";
                int statusColor = R.color.color_me_bg;
                switch (orderListBean.getOrderStatus()){
                    case 1: //已完成
                        statusStr = App.getContext().getString(R.string.order_status_done);
                        statusColor =R.color.complete_green;
                        break;
                    case 2: //订单处理中
                        statusStr = App.getContext().getString(R.string.order_status_dealing);
                        statusColor =R.color.doing;
                        break;
                    case 3: //已取消
                        statusStr = App.getContext().getString(R.string.order_status_cancel);
                        statusColor =R.color.cancel;
                        break;
                    case 4: //支付失败
                        statusStr = App.getContext().getString(R.string.order_status_defeat);
                        statusColor =R.color.pay_failed;
                        break;
                }
                String priceWithSymbol = "¥";
                if(orderListBean.getCurrency() == 1){
                    priceWithSymbol = "$";
                }else if(orderListBean.getCurrency() == 2){
                    priceWithSymbol = "¥";
                }else if(orderListBean.getCurrency() == 3){
                    priceWithSymbol = "";
                }
                priceWithSymbol = App.getContext().getString(R.string.order_pay, priceWithSymbol + orderListBean.getPrice());
                itemVH.orderStatus.setText(statusStr);
                itemVH.orderStatus.setTextColor(itemVH.orderStatus.getContext().getResources().getColor(statusColor));
                itemVH.orderPeriod.setText(App.getContext().getString(R.string.order_period, ""+orderListBean.getSetMealTime()));
                itemVH.orderPay.setText(priceWithSymbol);
                String payChannel = "";
                if("1".equals(orderListBean.getPayMethod())){
                    payChannel = App.getContext().getString(R.string.ali_pay);
                }else if("2".equals(orderListBean.getPayMethod())){
                    payChannel = ""/*App.getContext().getString(R.string.wechat)*/;
                }else if("3".equals(orderListBean.getPayMethod())){
                    payChannel = App.getContext().getString(R.string.apple_pay);
                }else if("4".equals(orderListBean.getPayMethod())){
                    payChannel = App.getContext().getString(R.string.google_pay);
                }else if("5".equals(orderListBean.getPayMethod())){
                    payChannel = App.getContext().getString(R.string.stripe_pay);
                }else if("6".equals(orderListBean.getPayMethod())){
                    payChannel = App.getContext().getString(R.string.wfee_pay);
                }

                itemVH.orderPayChannel.setText(App.getContext().getString(R.string.order_channel, payChannel));
                itemVH.orderId.setText(App.getContext().getString(R.string.order_id, orderListBean.getOrderId()));
                itemVH.orderPayTime.setText(App.getContext().getString(R.string.order_pay_time, orderListBean.getCreateTime()));
            }
        }

    }

    @Override
    public int getItemCount() {
        return orderInfos == null ? 0 : orderInfos.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(orderInfos.size() >= 11 && position + 1 == getItemCount()){
            return TYPE_FOOTER;
        }else {
            return TYPE_ITEM;
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView orderPkg;
        TextView orderStatus;
        TextView orderPeriod;
        TextView orderPay;
        TextView orderPayChannel;
        TextView orderId;
        TextView orderPayTime;

        public ItemViewHolder(View itemView) {
            super(itemView);

            orderPkg = (TextView) itemView.findViewById(R.id.order_package);
            orderStatus = (TextView) itemView.findViewById(R.id.order_status);
            orderPeriod = (TextView) itemView.findViewById(R.id.order_period);
            orderPay = (TextView) itemView.findViewById(R.id.order_pay);
            orderPayChannel = (TextView) itemView.findViewById(R.id.order_pay_channel);
            orderId = (TextView) itemView.findViewById(R.id.order_id);
            orderPayTime = (TextView) itemView.findViewById(R.id.order_pay_time);

        }

    }

    class FooterViewHolder extends RecyclerView.ViewHolder{

        TextView footerTip;

        public FooterViewHolder(View itemView) {
            super(itemView);

            footerTip = (TextView) itemView.findViewById(R.id.bottom_tip);
        }
    }


    public void updateDatas(List<OrderInfo.OrderListBean> list){
        orderInfos = list;
        notifyDataSetChanged();
    }

}
